#!/bin/bash

set -e

while getopts  "w:" opts
do
	case  $opts  in
	w)
		window=$OPTARG
	esac
done
shift $(($OPTIND - 1))

if [ -z "$2" ]; then
	echo 'hmmcopy.sh [-w window_size] <input.bam> <out.wig>'
	exit 1
fi

#-----------------------------------------------
#-----------------------------------------------
. /mnt/ilustre/app/medical/tools/.var #---------
#-----------------------------------------------
#-----------------------------------------------



log=.log
if [ ! -e "$log" ]; then
	:> $log
fi

bai=$1.bai
if [ ! -e "$bai" ]; then
	
	echo samtools index $1 2>>$log 1>&2
	samtools index $1
fi

echo 2>>$log 1>&2
echo 2>>$log 1>&2
echo readCounter 2>>$log 1>&2
echo window $window 2>>$log 1>&2
echo $1 2>>$log 1>&2
echo $2 2>>$log 1>&2
readCounter -w $window $1 > $2 2>>$log

